package cn.zshprint.serein.service.impl;

import cn.zshprint.serein.config.RedisConfig;
import cn.zshprint.serein.exception.ServiceException;
import cn.zshprint.serein.service.EsProductService;
import cn.zshprint.serein.service.RedisService;
import cn.zshprint.serein.vo.ProductVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zshprint.serein.entity.Product;
import cn.zshprint.serein.service.ProductService;
import cn.zshprint.serein.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品服务impl
 *
 * @author zshprint
 * @date 2021/07/28
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    /**
     * 产品映射器
     */
    @Resource
    private ProductMapper productMapper;
    /**
     * 复述,服务
     */
    @Resource
    private RedisService redisService;

    /**
     * 保存
     *
     * @param productVO 产品签证官
     * @return {@link Integer}
     */
    @Override
    public Integer save(ProductVO productVO) {
        Product target = new Product();
        BeanUtils.copyProperties(productVO, target);
        target.setCreateTime(LocalDateTime.now());
        int insert = productMapper.insert(target);
        if (insert <= 0) {
            throw new ServiceException("插入失败", 504);
        }
        redisService.set(RedisConfig.REDIS_KEY_DATABASE + "::product:" + target.getId(), target);
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:list");
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:allows");
        return target.getId();
    }

    /**
     * 更新
     *
     * @param product 产品
     * @return {@link Product}
     */
    @Override
    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product:'+#product.id")
    public Product update(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        boolean update = updateById(product);
        if (!update) {
            throw new ServiceException("更新失败", 505);
        }
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:list");
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:allows");
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:noAllows");
        redisService.set(RedisConfig.REDIS_KEY_DATABASE + "::product:" + product.getId(), product);
        return product;
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link Integer}
     */
    @Override
    @CacheEvict(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product:'+#id")
    public Integer delete(Integer id) {
        boolean update = update(new UpdateWrapper<Product>()
                .eq("id", id)
                .set("status", 0));
        if (!update) {
            throw new ServiceException("删除失败", 506);
        }
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:list");
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:allows");
        redisService.del(RedisConfig.REDIS_KEY_DATABASE + "::product:noAllows");
        return id;
    }

    /**
     * 选择所有允许
     *
     * @return {@link List<Product>}
     */
    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product:allows'")
    public List<Product> selectAllAllow() {
        return list(new QueryWrapper<Product>()
                .eq("status", 1));
    }

    /**
     * 选择所有
     *
     * @return {@link List<Product>}
     */
    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product:list'")
    public List<Product> selectAll() {
        return list();
    }

    /**
     * 选择一个
     *
     * @param id id
     * @return {@link Product}
     */
    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE, key = "'product:'+#id")
    public Product selectOne(Integer id) {
        return getById(id);
    }

    @Override
    @Cacheable(value = RedisConfig.REDIS_KEY_DATABASE,key = "'product:noAllows'")
    public List<Product> getNoAllow() {
        return list(new QueryWrapper<Product>()
                .eq("status", 0));
    }
}




